from django.shortcuts import render
from rest_framework.response import Response
from  rest_framework.views import APIView

from category import models
from category.categoryser import CategorySerializer


# Create your views here.
class Category(APIView):
    def get(self, request):
        allcate=models.CategoryModel.objects.filter(parent=None,ishome=True,isshow=True).order_by('order')
        ser=CategorySerializer(allcate,many=True)
        for i in ser.data:
            i['children']=i['children'][:3]
        return Response({'message':"ok",'data':ser.data,'code':200})

class NowCate(APIView):
    def get(self, request):
        id=request.query_params.get('id')
        info=models.CategoryModel.objects.filter(id=id,parent=None,ishome=True,isshow=True).first()
        ser=CategorySerializer(info)
        return Response({'message': "ok", 'data':ser.data, 'code': 200})